{% extends 'base/base.html' %}

{% block title %}
做菜指南
{% endblock %}

{% block main %}
<div class="d-flex justify-content-between align-items-center mb-4">
    <h2><i class="fas fa-utensils me-2"></i>做菜指南</h2>
    <div>
        <a href="{% url 'main:recipe_create' %}" class="btn btn-primary">
            <i class="fas fa-plus me-1"></i>新建做菜指南
        </a>
        <a href="{% url 'main:food_library' %}" class="btn btn-outline-secondary">返回食物库</a>
    </div>
</div>

<!-- 搜索和筛选 -->
<div class="card mb-4">
    <div class="card-body">
        <form method="get" class="row g-3">
            <div class="col-md-4">
                <div class="input-group">
                    <input type="text" class="form-control" name="search"
                           placeholder="搜索菜品..." value="{{ search_query }}">
                    <button class="btn btn-outline-primary" type="submit">
                        <i class="fas fa-search"></i>
                    </button>
                </div>
            </div>
            <div class="col-md-3">
                <select class="form-select" name="category">
                    <option value="">所有分类</option>
                    {% for category in categories %}
                    <option value="{{ category.id }}" {% if selected_category == category.id|stringformat:"s" %}selected{% endif %}>
                        {{ category.name }}
                    </option>
                    {% endfor %}
                </select>
            </div>
            <div class="col-md-3">
                <select class="form-select" name="difficulty">
                    <option value="">所有难度</option>
                    {% for value, label in difficulty_choices %}
                    <option value="{{ value }}" {% if selected_difficulty == value %}selected{% endif %}>
                        {{ label }}
                    </option>
                    {% endfor %}
                </select>
            </div>
            <div class="col-md-2">
                <button type="submit" class="btn btn-primary w-100">筛选</button>
            </div>
        </form>
    </div>
</div>

{% if recipes %}
<div class="row">
    {% for recipe in recipes %}
    <div class="col-md-6 col-lg-4 mb-4">
        <div class="card h-100 recipe-card">
            {% if recipe.image %}
            <img src="{{ recipe.image.url }}" class="card-img-top" alt="{{ recipe.name }}" style="height: 200px; object-fit: cover;">
            {% else %}
            <div class="card-img-top bg-light d-flex align-items-center justify-content-center" style="height: 200px;">
                <i class="fas fa-utensils fa-3x text-muted"></i>
            </div>
            {% endif %}
            
            <div class="card-body d-flex flex-column">
                <h5 class="card-title">{{ recipe.name }}</h5>
                {% if recipe.description %}
                <p class="card-text text-muted small">{{ recipe.description|truncatewords:15 }}</p>
                {% endif %}
                
                <div class="mb-3">
                    <span class="badge bg-primary me-1">{{ recipe.category.name }}</span>
                    <span class="badge bg-info me-1">{{ recipe.get_difficulty_display_cn }}</span>
                    <span class="badge bg-success">{{ recipe.get_total_cooking_time }}</span>
                </div>
                
                <div class="mb-3">
                    {% for tag in recipe.tags.all %}
                    <span class="badge me-1" style="background-color: {{ tag.color }};">{{ tag.name }}</span>
                    {% endfor %}
                </div>
                
                <div class="mt-auto">
                    <div class="d-flex justify-content-between align-items-center">
                        <small class="text-muted">
                            <i class="fas fa-user me-1"></i>{{ recipe.created_by.username|default:"系统" }}
                        </small>
                        <small class="text-muted">
                            <i class="fas fa-clock me-1"></i>{{ recipe.created_at|date:"m-d" }}
                        </small>
                    </div>
                    <div class="mt-2">
                        <a href="{% url 'main:recipe_detail' recipe.id %}" class="btn btn-sm btn-outline-primary">
                            <i class="fas fa-eye me-1"></i>查看详情
                        </a>
                        {% if recipe.created_by == request.user or request.user.is_superuser %}
                        <a href="{% url 'main:recipe_edit' recipe.id %}" class="btn btn-sm btn-outline-warning">
                            <i class="fas fa-edit me-1"></i>编辑
                        </a>
                        {% endif %}
                    </div>
                </div>
            </div>
        </div>
    </div>
    {% endfor %}
</div>
{% else %}
<div class="text-center py-5">
    <div class="mb-4">
        <i class="fas fa-utensils fa-4x text-muted"></i>
    </div>
    <h4 class="text-muted">暂无做菜指南</h4>
    <p class="text-muted">创建第一个做菜指南来分享您的烹饪心得</p>
    <a href="{% url 'main:recipe_create' %}" class="btn btn-primary">创建做菜指南</a>
</div>
{% endif %}
{% endblock %}

{% block js %}
<script>
// 自动提交筛选表单
document.querySelectorAll('select[name="category"], select[name="difficulty"]').forEach(select => {
    select.addEventListener('change', function() {
        this.form.submit();
    });
});
</script>
{% endblock %}

